package com.swipemaster.game.modes;

import android.app.Activity;

import com.google.analytics.tracking.android.EasyTracker;
import com.purplebrain.adbuddiz.sdk.AdBuddiz;
import com.swipemaster.activities.GameActivity;
import com.swipemaster.common.AdMobListener;
import com.swipemaster.dialogs.Dialogs;
import com.swipemaster.levels.Level;
import com.swipemaster.network.Network;
import com.swipemaster.network.task.IsWorldRecordTask;
import com.swipemaster.record.Record;
import com.swipemaster.record.RecordId;
import com.swipemaster.record.RecordsManager;

abstract public class GameMode
{
	public final RecordId recordId;

	private static final int AD_COUNTER_MAX = 5;
	private static int adCounter;

	public GameMode(RecordId recordId)
	{
		this.recordId = recordId;
	}

	public void addModePlayed(boolean isWinner)
	{
		if (isWinner)
		{
			EasyTracker.getTracker().setCustomMetric(1, 1L);
		}
	}

	abstract public Level getNewLevel();

	abstract public int getPlayedTimes();

	abstract public Record getRecord();

	abstract public String getYourScoreText();

	public void hideHelp()
	{
	}

	public boolean isTimeConstraint()
	{
		return false;
	}

	public boolean mustWinToUpdate()
	{
		return false;
	}

	public void onGameOver(final GameActivity activity, final boolean isWinner)
	{
		final Record record = getRecord();
		boolean isNewRecord = false;

		if (!mustWinToUpdate() || isWinner)
		{
			isNewRecord = RecordsManager.isNewRecordAndUpdate(record);
		}

		Dialogs.showGameDialog(activity, isWinner, isNextButton(isWinner), record, isNewRecord);

		if (isWinner && isNewRecord && Network.isConnected())
		{
			activity.runOnUiThread(new Runnable()
			{
				@Override
				public void run()
				{
					new IsWorldRecordTask(activity, record).execute();
				}
			});
		}

		if (isAdMoment())
		{
			AdBuddiz.getInstance().showAd(AdMobListener.AD_2);
		}
	}

	private boolean isAdMoment()
	{
		return (++adCounter % AD_COUNTER_MAX == 0);
	}

	public void onNext()
	{
	}

	abstract public void playSong();

	public void showHelp(Activity activity)
	{
	}

	@Override
	public String toString()
	{
		return recordId.toString();
	}

	boolean isNextButton(boolean isWinner)
	{
		return false;
	}
}
